package com.hai.bean;

import java.util.List;

import com.hai.qtalk.ChatActivity;
import com.hai.qtalk.FragmentTabs;
import com.hai.qtalk.Login;
import com.hai.qtalk.MainActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

	private LayoutInflater mInflater;
	private List k;
	private FragmentTabs activity;
	
	public MyExpandableListAdapter(FragmentTabs activity){
		this.activity = activity;
	}
	
	public MyExpandableListAdapter(Context context,List k){
		this.mInflater=LayoutInflater.from(context);
		this.k=k;
	}
	
	public MyExpandableListAdapter(Context context){
		this.mInflater=LayoutInflater.from(context);
	}
	
	
	@Override
	public int getGroupCount() {
		return activity.getGroupArray().size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return activity.getChildArray().get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return activity.getChildArray().get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		return getGenericView(activity.getGroupArray().get(groupPosition));
	}

	private TextView getGenericView(String s){
		AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT );
		TextView textView = new TextView(activity);
		textView.setLayoutParams(layoutParams);
		
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		
		textView.setPadding(40, 0, 0, 0);
		textView.setText(s);
		
		return textView;
	}
	
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View childView =getGenericView(activity.getChildArray().get(groupPosition).get(childPosition));
		childView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String name=(String) ((TextView)v).getText();
				Bundle data = new Bundle();
				data.putSerializable("name",  name);
				// 启动Main Activity
				Intent intent = new Intent(activity,
						ChatActivity.class);
				intent.putExtras(data);
				activity.startActivity(intent);
				
			}
		});
		return childView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}
	
}
